package com.example.springbootlocal.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootlocal.common.Result;
import com.example.springbootlocal.domain.Student;
import com.example.springbootlocal.service.StudentService;
import com.example.springbootlocal.utils.page.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author X.I.O
 * @title: StudentController
 * @projectName spring-boot-local
 * @description: TODO
 * @date 2021/11/9 16:16
 */
@Api(tags = {"student操作块"})
@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/save")
    @ApiOperation("新增student")
    public JSONObject saveStudentController(@RequestBody Student student){
        studentService.saveStudent(student);
        return Result.successInfo();
    }

    @GetMapping("/get")
    @ApiOperation("获取student")
    public JSONObject getStudentController(@ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        List<Student> studentList=studentService.getStudentListByKeyWord(keyword);
        JSONObject object=new JSONObject();
        object.put("data",studentList);
        return object;
    }


    @ApiOperation("分页测试")
    @GetMapping("/page")
    public IPage<Student> getStudentController(@ApiParam("页码")@RequestParam(value = "page",defaultValue = "1")Integer page,
                                               @ApiParam("条数")@RequestParam(value = "size",defaultValue = "10")Integer size){
        IPage<Student> studentIPage=studentService.getIPageController(page,size);
        return studentIPage;
    }

    @ApiOperation("测试自增id")
    @PostMapping("/autoid")
    public JSONObject autoIdController(@RequestBody Student student){
        System.out.println(student+"学生");
        studentService.autoNumSaveStudent(student);
        return Result.successInfo();
    }

    @ApiOperation("测试删除")
    @PostMapping("/delete")
    public JSONObject deleteController(@RequestParam("id")Integer id){
        studentService.deleteById(id);
        return Result.successInfo();
    }


    @ApiOperation("更新")
    @PostMapping("/update")
    public JSONObject updateController(@RequestBody Student student){
        studentService.updateById(student);
        return Result.successInfo();
    }


}
